package com.easy.controller;

import com.easy.bean.User;
import com.easy.service.UserService;
import com.easy.util.Page;
import com.easy.util.PasswordUtil;
import com.easy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin//允许跨域
@RequestMapping("user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping
    public Result add(@RequestBody User user){
        user.setUser_password(PasswordUtil.getEncryption(user.getUser_password()));
        Object obj=userService.add(user);
        return Result.success(obj);
    }

    @PutMapping
    public Result edit(@RequestBody User user){
        if (user.getUser_password()!=null){
            user.setUser_password(PasswordUtil.getEncryption(user.getUser_password()));
        }
        Object obj=userService.edit(user);
        return Result.success("编辑信息保存成功",obj);
    }

    @DeleteMapping("{id}")
    public Result del(@PathVariable("id") Integer id){
        Object obj=userService.del(id);
        return Result.success("删除成功",obj);
    }

    @GetMapping("{id}")
    public Result one(@PathVariable("id") Integer id){
        Object obj=userService.one(id);
        return Result.success(obj);
    }

    @GetMapping("all")
    public Result all(User user){
        Object obj=userService.all(user);
        return Result.success(obj);
    }

    @GetMapping("page")
    public Result page(Page page,User user){
        //查询一共有多少条数据
        //查询当前页要显示的数据
        //将以上数据封装到page对象中
        userService.page(page,user);
        //返回page对象
        return Result.success(page);

    }

}
